#ifndef DO3DTYPETRAITS_H
#define DO3DTYPETRAITS_H

/*
Copyright (c) 2013, ebaklund
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.

3. Neither the name of the <ORGANIZATION> nor the names of its contributors 
   may be used to endorse or promote products derived from this software 
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

#include <memory>


namespace Do
{
  template<typename _DecayedT>
  struct _is_shared_ptr 
    : std::false_type {
  };

  template<typename _DecayedT>
  struct _is_shared_ptr<std::shared_ptr<_DecayedT>> 
    : std::true_type {
  };

  template<typename _T>
  struct is_shared_ptr 
    : _is_shared_ptr<typename std::decay<_T>::type> {
  };

  template<typename _DecayedT>
  struct _is_unique_ptr 
    : std::false_type {
  };

  template<typename _DecayedT>
  struct _is_unique_ptr<std::unique_ptr<_DecayedT>> 
    : std::true_type {
  };

  template<typename _T>
  struct is_unique_ptr 
    : _is_unique_ptr<typename std::decay<_T>::type> {
  };

  template<typename _DecayedT>
  struct _is_weak_ptr 
    : std::false_type {
  };

  template<typename _DecayedT>
  struct _is_weak_ptr<std::weak_ptr<_DecayedT>> 
    : std::true_type {
  };

  template<typename _T>
  struct is_weak_ptr 
    : _is_weak_ptr<typename std::decay<_T>::type> {
  };

  template<typename _DecayedT>
  struct _is_smart_ptr {
    static bool const value =
      _is_shared_ptr<_DecayedT>::value ||
      _is_unique_ptr<_DecayedT>::value ||
      _is_weak_ptr<_DecayedT>::value;
  };

  template<typename _T>
  struct is_smart_ptr 
    : _is_smart_ptr<typename std::decay<_T>::type> {
  };

  template<typename _T, typename _DecayedT>
  struct _is_valid_callback_signature {
    static bool const value =
      (is_same<_T, _DecayedT>::value || is_same<_T, _DecayedT const&>::value) &&
      !std::is_pointer<_DecayedT>::value &&
      !_is_smart_ptr<_DecayedT>::value;
  };

  template<typename _A, typename... _Args>
  struct is_valid_callback_signature
    : _is_valid_callback_signature<_A, typename std::decay<_A>::type> {
  };

  template<typename _T>
  struct is_valid_callback_signature<_T> 
    : _is_valid_callback_signature<_T, typename std::decay<_T>::type> {
  };

  template<int pos, typename A, typename... Args>
  void static_assert_valid_callback_signature();

  template<int pos>
  void static_assert_valid_callback_signature() {
  };

  template<int pos, typename A, typename... Args>
  void static_assert_valid_callback_signature()
  {
    static int const argument_position = pos;

    static_assert(
      !std::is_rvalue_reference<A>::value, 
      "\n\t\t'&&' Argument is rvalue. "
      "Canonical do:: callback arguments must be passed by value 'T', "
      "or passed by lvalue const reference 'T const&'.");

    static_assert(
      !std::is_pointer<std::decay<A>::type>::value, 
      "\n\t\t'*' Argument decays to pointer. "
      "Canonical do:: callback arguments must be passed by value 'T', "
      "or passed by lvalue const reference 'T const&'.");

    static_assert(
      (!std::is_lvalue_reference<A>::value) || std::is_const<std::remove_reference<A>::type>::value, 
      "\n\t\t'&' Argument is mutable lvalue. "
      "Canonical do:: callback arguments must be passed by value 'T', "
      "or passed by lvalue const reference 'T const&'.");

    static_assert(
      !Do::is_smart_ptr<std::decay<A>::type>::value,
      "\n\t\t'smart ptr <T>' Argument is smart pointer. "
      "Canonical do:: callback arguments must be passed by value 'T', "
      "or passed by lvalue const reference 'T const&'.");

    static_assert_valid_callback_signature<pos+1, Args...>();
  };

}

#endif // DO3DTYPETRAITS_H